package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.service.SensitiveService;
import com.heima.admin.mapper.SensitiveMapper;
import com.heima.model.admin.dtos.AdSensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class SensitiveServiceImpl extends ServiceImpl<SensitiveMapper, AdSensitive> implements SensitiveService {

    @Override
    public ResponseResult findByNameAndPage(AdSensitiveDto adSensitiveDto) {
        //1.检查参数是否正确
        adSensitiveDto.checkParam();
        //2.封装查询条件(查询参数\分页参数)
        IPage<AdSensitive> pageRequest = new Page<>(adSensitiveDto.getPage(), adSensitiveDto.getSize());

        LambdaQueryWrapper<AdSensitive> queryWrapper = Wrappers.<AdSensitive>lambdaQuery();
        //判断查询条件非空
        if(StringUtils.isNotBlank(adSensitiveDto.getName())){
            queryWrapper.like(AdSensitive::getSensitives,adSensitiveDto.getName());
        }

        //3.得到分页结果
        IPage<AdSensitive> pageResult = this.page(pageRequest,queryWrapper);

        //4.封装PageResponseResult
        PageResponseResult pageResponseResult = new PageResponseResult(adSensitiveDto.getPage(), adSensitiveDto.getSize(), pageResult.getTotal());
        pageResponseResult.setData(pageResult.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //1.检查参数
        if (StringUtils.isBlank(adSensitive.getSensitives())) {
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "敏感词内容不能为空");
        }
        //2.检查是否重复
        LambdaQueryWrapper<AdSensitive> queryWrapper = Wrappers.<AdSensitive>lambdaQuery();
        if (count(queryWrapper.eq(AdSensitive::getSensitives, adSensitive.getSensitives()))>0){
            ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "敏感词已经存在");
        }
        //3.插入数据,返回
        this.insert(adSensitive);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //1.检查参数
        if (adSensitive.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //2.检查修改后的参数是否重复
        LambdaQueryWrapper<AdSensitive> queryWrapper = Wrappers.<AdSensitive>lambdaQuery();
        if (count(queryWrapper.eq(AdSensitive::getSensitives, adSensitive.getSensitives()))>0){
            ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "要修改敏感词已经存在");}
        //3.修改
        this.updateById(adSensitive);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delete(Integer id) {
            AdSensitive adSensitive = getById(id);
            if(adSensitive==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,
                        "要删除的敏感词不存在"); }
            this.removeById(adSensitive.getId());
            return ResponseResult.okResult();
    }
}
